<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item>
        <h1 class="title">华盛集团</h1>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input clearable v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import img from "../assets/img/4.jpg";
import { userlogin } from "../request/api";
export default {
  data() {
    return {
      ruleForm: {
        password: "admin888",
        username: "admin",
      },
      rules: {
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    document.querySelector(
      ".login"
    ).style.cssText = `background: url(${img}) no-repeat; background-size: cover;`;
    document.querySelector("#app").style.cssText = `opacity: 1;`;
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((val) => {
        if (!val) {
          return;
        }
        userlogin(this.ruleForm).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.$store.dispatch("setUserInfo", res.list);
            this.$router.push("/home");
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
}
.demo-ruleForm {
  width: 500px;
  height: 250px;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}
.el-button {
  width: 100%;
}
.title {
  text-align: center;
  font-size: 50px;
  margin-bottom: 20px;
}
</style>